<?php
namespace app\common\library;
use think\facade\Config;
class DES3{
    /*
     * 接口数据加密类；
     * @Time:2015-10-12
     * @Author:Victor
	  * 算法：3des 
     * 加密模式：cbc 
     * 补齐方法：PKCS7 
     */
    // var $key = "gd9EbxrC9bSl9zxcciclwQKO";
    // var $key = "my.oschina.net/penngo #@";
    var $iv = "01234567";
    #加密
    //必须输入的为utf-8格式的数据，否则会出错
    /*
     * @parm $key,   密钥
     * $parm $input  字符串
     */
    function encrypt($key,$input){
        $key=$key?$key:Config::get('scj.key_3des');
        $size = mcrypt_get_block_size(MCRYPT_3DES,MCRYPT_MODE_CBC);
        $input = $this->pkcs5_pad($input, $size);
        $key = str_pad($key,24,'0');
        $td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
        if( $this->iv == '' ){
            $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        }else{
            $iv = $this->iv;
        } 
        @mcrypt_generic_init($td, $key, $iv);
        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = base64_encode($data);
        return $data;
    }
  
    #解密
    function decrypt($key,$encrypted){
        $key=$key?$key:Config::get('scj.key_3des');
        $encrypted = base64_decode($encrypted);
        $key = str_pad($key,24,'0');
        $td = mcrypt_module_open(MCRYPT_3DES,'',MCRYPT_MODE_CBC,'');
        if( $this->iv == '' ){
            $iv = @mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        }else{
            $iv = $this->iv;
        } 
        $ks = mcrypt_enc_get_key_size($td);
        @mcrypt_generic_init($td, $key, $iv);
        $decrypted = mdecrypt_generic($td, $encrypted);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $y=$this->pkcs5_unpad($decrypted);
	// $ob = new \Think\CharsetConv('utf-8', 'ansi');
	// $y = $ob->convert($y);
        $ob=null;
        return $y;
    }
    
    
    function pkcs5_pad ($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }
    
    
    function pkcs5_unpad($text){
        $pad = ord($text{strlen($text)-1});
        if ($pad > strlen($text)) {
            return false;
        }
        if(strspn($text, chr($pad), strlen($text) - $pad) != $pad){
            return false;
        }
        return substr($text, 0, -1 * $pad);
    }
	
    function PaddingPKCS7($data) {
        $block_size = mcrypt_get_block_size(MCRYPT_3DES, MCRYPT_MODE_CBC);
        $padding_char = $block_size - (strlen($data) % $block_size);
        $data .= str_repeat(chr($padding_char),$padding_char);
        return $data;
    }
  
    function showResult($message,$status,$data){
        if(empty($data)){
        $data='';
        }
        $myarray=array("message"=>$message,"status"=>$status,'data'=>$data);
        $string=json_encode($myarray);
        echo $data=$this->encrypt(KEY,$string);
        // echo "<br>".$string;
    }
}